package cn.linran.order.service.impl;

import cn.linran.order.domain.OrderPetAcquisition;
import cn.linran.order.dto.OrderPetAcquisitionDto;
import cn.linran.order.mapper.OrderPetAcquisitionMapper;
import cn.linran.order.service.IOrderPetAcquisitionService;
import cn.linran.basic.service.impl.BaseServiceImpl;
import cn.linran.pet.domain.Pet;
import cn.linran.pet.mapper.PetMapper;
import cn.linran.pet.mapper.SearchMasterMsgMapper;
import cn.linran.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IPetService petService;

    /**
     * 取消订单
     *
     * @param id
     */
    @Override
    public void cancel(Long id) {
        /**
         * 1. 修改订单状态
         * 2. 寻主消息的shopid=null
         * 3. pet 删除
         */
        // 1. 修改订单状态
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(-1); // 取消
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        // 2. 调用reject方法，将寻主消息的shopid = null
        searchMasterMsgMapper.reject(orderPetAcquisition.getSearchMasterMsgId());

        // 3. pet 删除  ,因为删除宠物的时候还要删除宠物详细信息。
        petService.remove(orderPetAcquisition.getPetId());
    }
    /**
     * 分页拥有参数的分页query(empId,shopId)
     */
//    @Override
//    public PageList<OrderPetAcquisition> pageQuery(OrderPetAcquisitionQuery query, Long id) {
//        /**
//         * shopId
//         * empId
//         */
//        Employee employee = employeeMapper.loadByLogininfoId(id);
//        if(employee.getShop_id()!=null){
//
//            Shop shop = shopMapper.loadById(employee.getShop_id());
//            if(shop.getAdmin_id()!=employee.getId()){
//                query.setEmpId(employee.getId());
//            }
//            // 店铺人员
//            query.setShopId(employee.getShop_id());
//        }
//
//        return super.pageQuery(query);
//    }
//

    /**
     * 订单确认
     * @param dto
     */
    @Override
    public void confirm(OrderPetAcquisitionDto dto) {
        /**
         * 1. 改订单状态，金额, 支付方式
         * 2. 改寻主消息状态
         * 3. 改pet金额
         */
        Long ordreId = dto.getId();
        BigDecimal money = dto.getMoney();
        Integer payType = dto.getPayType();

        // 1.改订单状态，金额, 支付方式
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(ordreId);
        orderPetAcquisition.setState(2);
        orderPetAcquisition.setPrice(money);
        orderPetAcquisition.setPaytype(payType);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        // 2.改寻主消息状态
        searchMasterMsgMapper.updateStateBySmmId(orderPetAcquisition.getSearchMasterMsgId());

        // 3. 改pet金额
        // 3.1根据id查找
        Pet pet = petMapper.loadById(orderPetAcquisition.getPetId());
        pet.setCostprice(money);
        petMapper.update(pet);

    }


}
